//
//  TYAlertViewHelper.swift
//  CommonTools
//
//  Created by xianquan on 2023/12/30.
//

import Foundation
import TYAlertView

public extension TYAlertView {
    
    struct Strong{
        let image: UIImage?
        let countDown: Int
        public init(image: UIImage?, countDown: Int) {
            self.image = image
            self.countDown = countDown
        }
    }
    
    struct ExtraInfo{
        let subTitle: String
        let messageTextAlignment: NSTextAlignment
        let isDismissOnAction: Bool
        public init(subTitle: String, messageTextAlignment: NSTextAlignment, isDismissOnAction: Bool) {
            self.subTitle = subTitle
            self.messageTextAlignment = messageTextAlignment
            self.isDismissOnAction = isDismissOnAction
        }
    }
    
    enum AlertType {
        case common
        case delete
        case strong(info: Strong)
    }
    
    @discardableResult static func showCommonAlert(title: String? = nil, message: String?, style: TYAlertViewStyle = .alert, buttonTitles: [String]?, cancelButtonTitle: String?, handler: @escaping TYAlertViewActionHandler) -> TYAlertView{
        let alertView = commonAlert(title: title, message: message, alertType: .common,  style: style, buttonTitles: buttonTitles, cancelButtonTitle: cancelButtonTitle, handler: handler)
        alertView.show()
        return alertView
    }
    
    @discardableResult static func showSubCommonAlert(title: String, extraInfo: ExtraInfo, message: String?, style: TYAlertViewStyle = .alert, buttonTitles: [String]?, cancelButtonTitle: String?, handler: @escaping TYAlertViewActionHandler) -> TYAlertView{
        let alertView = commonAlert(title: title, extraInfo: extraInfo, message: message, alertType: .common,  style: style, buttonTitles: buttonTitles, cancelButtonTitle: cancelButtonTitle, handler: handler)
        alertView.show()
        return alertView
    }
    
    @discardableResult static func showDeleteAlert(title: String? = nil, message: String?, buttonTitles: [String]?, cancelButtonTitle: String?, handler: @escaping TYAlertViewActionHandler)  -> TYAlertView{
        let alertView = commonAlert(title: title, message: message, alertType: .delete, style: .alert, buttonTitles: buttonTitles, cancelButtonTitle: cancelButtonTitle, handler: handler)
        alertView.show()
        return alertView
    }
    
    @discardableResult static func showStrongAlert(title: String? = nil, message: String?, strong: Strong = Strong(image: .imageIn(bundleName: .commonTools("icon_prompt_yellow"), originSize: CGSize(width: 24, height: 24)), countDown: 5), buttonTitles: [String]?, cancelButtonTitle: String?, handler: @escaping TYAlertViewActionHandler)  -> TYAlertView{
        let alertView = commonAlert(title: title, message: message, alertType: .strong(info: strong), style: .alert, buttonTitles: buttonTitles, cancelButtonTitle: cancelButtonTitle, handler: handler)
        alertView.show()
        return alertView
    }
    
    private static func commonAlert(title: String?, extraInfo: ExtraInfo? = nil, message: String?, alertType: AlertType, style: TYAlertViewStyle, buttonTitles: [String]?, cancelButtonTitle: String?, handler: @escaping TYAlertViewActionHandler) -> TYAlertView {
        let shouldCustom = buttonTitles?.count ?? 0 == 1 && style != .actionSheet
        let alertView = TYAlertView(title: title, message: message, style: style, buttonTitles: shouldCustom ? nil : buttonTitles, cancelButtonTitle: shouldCustom ? nil : cancelButtonTitle , actionHandler: handler)
        if let extraInfo = extraInfo {
            alertView.subTitle = extraInfo.subTitle
            alertView.subTitleFont = adapt(16).medium
            alertView.subTitleTextColor = .black9
            alertView.subTitleSpacing = adapt(10)
            alertView.messageTextAlignment = extraInfo.messageTextAlignment
            alertView.isDismissOnAction = extraInfo.isDismissOnAction
        }
        alertView.titleFont = adapt(18).medium
        alertView.cancleFont = adapt(16).medium
        alertView.otherFont = adapt(16).medium
        alertView.messageFont = title == nil ? adapt(16).medium : adapt(16).regular
        alertView.messageTextColor = .black3
        alertView.buttonHeight = adapt(40)
        alertView.innerMarginHeight = adapt(28)
        if style == .alert {
            alertView.layerCornerRadius = adapt(20)
            if isIpad {
                alertView.width = adapt(380)
            }
            else{
                alertView.width = screenWidth() - adapt(55)
            }
        }
        else {
            return alertView
        }
        if let buttonTitles = buttonTitles, buttonTitles.count == 1, let sureTitle = buttonTitles.first {
            alertView.shouldHideLine = true
            alertView.spacingBetweenContentAndButton = adapt(10)
            
            let cancelAction = TYAlertAction()
            cancelAction.title = cancelButtonTitle

            cancelAction.width = adapt(108)
            cancelAction.offsetX = adapt(14)
            cancelAction.cornerRadius = adapt(20)
            cancelAction.isCancel = true
            cancelAction.verticalSpacing = adapt(28)
            
            let sureAction = TYAlertAction()
            sureAction.title = sureTitle

            sureAction.cornerRadius = adapt(20)
            sureAction.width = adapt(108)
            sureAction.offsetX = adapt(-14)
            sureAction.isCancel = false
            
            
            switch alertType {
            case .common:
                cancelAction.backgroundColor = .selectedBg
                cancelAction.backgroundColorHighlighted = .selectedBg
                cancelAction.titleColor = .selectedColor
                sureAction.backgroundColor = .selectedColor
                sureAction.backgroundColorHighlighted = .selectedColor
                sureAction.titleColor = .white
            case .delete:
                cancelAction.backgroundColor = UIColor.ty_color(withHexString: "#999999", alpha: 0.12)
                cancelAction.backgroundColorHighlighted = cancelAction.backgroundColor
                cancelAction.titleColor = .black6
                sureAction.backgroundColor = .deleteAlert
                sureAction.backgroundColorHighlighted = .deleteAlert
                sureAction.titleColor = .white
            case let .strong(info):
                cancelAction.backgroundColor = .selectedBg
                cancelAction.backgroundColorHighlighted = .selectedBg
                cancelAction.titleColor = .selectedColor
                sureAction.backgroundColor = .selectedColor
                sureAction.backgroundColorHighlighted = .selectedColor
                sureAction.titleColor = .white
                sureAction.countDown = info.countDown
                alertView.titleImage = info.image
                
            }
            if let cancelButtonTitle = cancelButtonTitle {
                let cancelWidth = (cancelAction.countDown > 0 ? "\(cancelButtonTitle) \(cancelAction.countDown)s" : cancelButtonTitle).width(for: adapt(16).medium)
                let sureWidth = (sureAction.countDown > 0 ? "\(sureTitle) \(sureAction.countDown)s" : sureTitle).width(for: adapt(16).medium)
                let titleWidth = max(cancelWidth, sureWidth)
                cancelAction.width = max(titleWidth + adapt(32), adapt(108))
                sureAction.width = cancelAction.width
                cancelAction.offsetX = adapt(14) - (cancelAction.width - adapt(108)) / 2
                sureAction.offsetX = -cancelAction.offsetX
                alertView.add([cancelAction, sureAction])
            }
            else {
                sureAction.width = alertView.width - adapt(80)
                sureAction.offsetX = 0
                sureAction.verticalSpacing = adapt(28)
                alertView.add([sureAction])
            }
        }
        
        return alertView
    }
}
